<template>
  <el-dialog
    :type="type"
    :title="title"
    :append-to-body="appendToBody"
    :visible.sync="visible"
    :close-on-click-modal="closeOnClickModal"
    :width="width"
  >
    <div class="input-box flexbox">
      <div
        class="code-item"
        :class="codeValue.length == 0 && inputFocus ? 'code-item-active' : ''"
      >
        {{ codeValue[0] }}
      </div>
      <div
        class="code-item"
        :class="codeValue.length == 1 && inputFocus ? 'code-item-active' : ''"
      >
        {{ codeValue[1] }}
      </div>
      <div
        class="code-item"
        :class="codeValue.length == 2 && inputFocus ? 'code-item-active' : ''"
      >
        {{ codeValue[2] }}
      </div>
      <div
        class="code-item"
        :class="codeValue.length == 3 && inputFocus ? 'code-item-active' : ''"
      >
        {{ codeValue[3] }}
      </div>
      <el-input
        maxlength="4"
        minlength="4"
        class="input-code"
        :value="codeValue"
        @blur="codeInputBlur"
        @focus="codeInputFocus"
        @input="codeInputChange"
      ></el-input>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button class="btn-red" type="primary" @click="dataFormSubmit">
        确 定
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    appendToBody: { type: Boolean, default: false }, // 是否嵌套
    closeOnClickModal: { type: Boolean, default: false }, // 是否可以点击关闭
    title: String, // 对话框title
    type: String, // 对话框类型：1.基础表单[base-dialog-form] 2.表格[base-dialog-table] 3.全屏 [base-dialog-fullscreen]
    width: String, // 对话框宽度800px 或 50%
  },
  data() {
    return {
      visible: false,
      codeValue: '',
      inputFocus: false,
      sendCodeFlag: false,
    }
  },

  methods: {
    init() {
      this.visible = true
    },
    // 输入框
    codeInputChange(e) {
      if (e) {
        // 判断输入内容是否为数字
        if (/^\+?[0-9][0-9]*$/.test(e)) {
          this.codeValue = e
        } else {
          this.$message.error('请输入数字')
        }
      } else {
        this.codeValue = ''
      }
    },
    // 输入框失去焦点
    codeInputBlur() {
      this.inputFocus = false
    },
    // 输入框获取到焦点
    codeInputFocus() {
      this.inputFocus = true
    },
    dataFormSubmit() {
      this.$emit('pwValue', this.codeValue)
      this.visible = false
    },
  },
}
</script>
<style lang="scss" scoped>
.input-box {
  position: relative;
  display: flex;
  margin-left: 50px;

  .code-item {
    width: 50px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    border: 1px solid #cec8c8;
    margin-right: 10px;
  }

  .code-item-active {
    border: 1px solid #f23026;
    box-sizing: border-box;
  }
  .input-code {
    position: absolute;
  }
}

::v-deep .el-input__inner {
  width: 240px;
  height: 50px;
  background-color: transparent;
  border: none;
  color: transparent;
}
::v-deep .el-dialog__body {
  padding: 0 20px;
}
::v-deep .el-dialog__footer {
  padding: 10px 20px 10px;
}
</style>
